package com.awaken.content.controller;

import com.awaken.content.models.dto.UserDTO;
import com.awaken.content.models.request.UserRequest;
import com.awaken.content.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户控制器 - 提供用户相关的HTTP接口
 * 这个类只负责接收HTTP请求和返回响应，具体的业务逻辑调用服务层
 */
@Slf4j
@Tag(name = "用户管理", description = "用户相关接口")
@RestController
@RequestMapping("/api/user")
@Validated
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "用户注册接口")
    public ResponseEntity<UserDTO> register(@RequestBody @Valid UserRequest request) {
        log.info("收到用户注册请求: {}", request.getUsername());
        UserDTO response = userService.register(request);
        return ResponseEntity.ok(response);
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录接口")
    public ResponseEntity<UserDTO> login(@RequestBody @Valid UserRequest request) {
        log.info("收到用户登录请求: {}", request.getUsername());
        UserDTO response = userService.login(request);
        return ResponseEntity.ok(response);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取用户信息", description = "根据用户ID获取用户信息")
    public ResponseEntity<UserDTO> getUserById(@PathVariable Long id) {
        log.info("获取用户信息: {}", id);
        UserDTO response = userService.getUserById(id);
        return ResponseEntity.ok(response);
    }

    @GetMapping("/list")
    @Operation(summary = "获取所有用户", description = "获取所有用户列表")
    public ResponseEntity<List<UserDTO>> getAllUsers() {
        log.info("获取所有用户列表");
        List<UserDTO> response = userService.getAllUsers();
        return ResponseEntity.ok(response);
    }
} 